package android.tong;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MenuList extends Activity implements OnItemClickListener {

	public static final int RELOAD_ID = Menu.FIRST;
	public static final int EXIT_ID = Menu.FIRST + 1;
	ListView lv;
	ArrayAdapter<String> adapter;
	DataModel dm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		dm = new DataModel();
		lv = new ListView(this);
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dm.loadData());
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
		setContentView(lv);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		setTitle("choice: " + String.valueOf(dm.getPrice(arg2)));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0, RELOAD_ID, 0, "Reload");
		MenuItem im = menu.findItem(RELOAD_ID);
		im.setIcon(R.drawable.tt);
		menu.add(0, EXIT_ID, 1, "Exit");
		im = menu.findItem(EXIT_ID);
		im.setIcon(R.drawable.ts);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case RELOAD_ID:
			adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dm.loadData());
			lv.setAdapter(adapter);
			break;
		case EXIT_ID:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
